<?php
/**
 * Created by PhpStorm.
 * User: o0无忧亦无佈(285753421@qq.com)
 * Date: 15-2-2
 * Time: 下午5:25
 */

class files extends MY_User_Controller{
    protected $user_id;
    protected $list_row;
    public function __construct()
    {
        parent::__construct();
        $this->load->model('user/user_model');
        $this->load->model('user/user_file');
        $this->user_id = $this->_login_user['user_id'];
        $this->config->load('document', TRUE);
        $this->list_row	= $this->config->item('list_row', 'document');
    }

    function my_file($page = 1,$classify = 0){
        $this->assign('head_title', '我的文档 - 我的账号');
        $offset = ($page-1)*$this->list_row;
        $condition = array(
            'user_id'   => $this->user_id,
            'file_classify' => $classify
        );
        $my_file = $this->user_file->get_file($condition,$this->list_row,$offset);
        for($i=0;$i<count($my_file);$i++){
            $my_file[$i]['collect'] = $this->user_file->get_like_by_id($this->user_id,$my_file[$i]['id'],1);
            $my_file[$i]['like'] = $this->user_file->get_like_by_id($this->user_id,$my_file[$i]['id'],1);
        }

        $this->assign('file_list',$my_file);
        $this->display();
    }

    function del($file_id){
        if(!$file_id && is_integer(intval($file_id))){
            $this->json_response(false, array('field' => 'file_id'), '文件没有被选中');
        }

        $result = $this->user_file->soft_del($this->user_id,$file_id);
        if(!$result){
            $this->json_response(false,array(),'删除失败,联系管理员');
        }

        $this->json_response(true,array('del'=>1,'file_id'=>$file_id),'删除成功');
    }

    /*
     * 文件归档
     * 将文件分配分类属性
     */
    function place_file(){
        $this->assign('head-title','文件归档 - 我的文档');
        $this->display();
    }

    function do_place_file(){
        $file_id = intval(trim($this->input->post('file_id')));
        $classify_id = intval(trim($this->input->post('classify_id')));
        if(!$file_id or !$classify_id){
            $this->json_response(false,array('id'=>'lost'),'丢失文件ID或者分类ID');
        }

        $result = $this->user_file->set_file_classify($file_id,$classify_id);

        if(!$result){
            $this->json_response(false,array(),'归档失败，联系管理员');
        }

        $this->json_response(true,array('del'=>1,'file_id'=>$file_id,'classify_id'=>$classify_id),'文件归档成功');
    }

    function edit($file_id){
        if($file_id<1)
            exit('错误');
        $this->assign('head_title', '编辑文档信息 - 我的账号');
        $file_info = $this->user_file->get_file_by_id($this->user_id,$file_id);
        $this->assign('file',$file_info);
        $this->display();
    }

    function do_edit(){
        $data = array(
            'id'    => intval($this->input->post('id')),
            'title' => trim($this->input->post('title')),
            'intro' => trim($this->input->post('intro')),
            'tag' => trim($this->input->post('tag')),
            'thumbnail' => trim($this->input->post('thumbnail')),
            'file_classify' => intval($this->input->post('file_classify'))
        );
        $result = $this->user_file->updata_file($data);

        if(!$result){
            $this->return_json(false,'','更新文件信息失败');
        }
        $this->return_json(true,'','更新文件信息成功');
    }
}